package com.strelitzia.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.strelitzia.admin.Entity.Dynamic;
import com.strelitzia.admin.Entity.DynamicEntity;
import com.strelitzia.admin.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @create 2024/1/23 15:10
 */
@Controller
public class DynamicController {
    @Autowired
    DynamicService dynamicService;

    @GetMapping("/dynamic/{page}")
    public String dynamic(@PathVariable("page") int page, Model model) {

        Page<DynamicEntity> dynamicEntityPage = dynamicService.page(new Page<>(page, 10), new QueryWrapper<DynamicEntity>().orderByDesc("publish_time"));
        model.addAttribute("dynamics", dynamicEntityPage);
        model.addAttribute("page", page);
        return "dynamic";
    }

    @GetMapping("/dynamic/details/{id}")
    public String getDynamic(@PathVariable("id") int id, Model model) {
        DynamicEntity byId = dynamicService.getById(id);
        Dynamic dynamic = new Dynamic();
        dynamic.setContent(byId.getContent());
        dynamic.setTime(byId.getPublishTime());
        if(byId.getAttachments()!=null) {
            dynamic.setSrc(byId.getAttachments().split(","));
        }
        model.addAttribute("dynamic", dynamic);
        return "DynamicDetails";
    }

    @GetMapping("/dynamic/del/{id}")
    public String del(@PathVariable("id") int id) {
        dynamicService.removeById(id);
        return "redirect:/dynamic/1";
    }
}
